package com.elot.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.stereotype.Service;

import com.elot.core.dao.GenericDao;
import com.elot.dto.UserDto;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private ShaPasswordEncoder passwordEncoder;
	@Autowired
	private GenericDao genericDao;

	public UserDto find(Long id) {
		return genericDao.findObject(UserDto.class, id);
	}

	public void remove(Long id) {
		genericDao.removeObject(UserDto.class, id);
	}

	public UserDto save(UserDto user) {
		user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
		return genericDao.saveObject(user);
	}

	public Collection<UserDto> findAll() {
		return genericDao.findAllObjects(UserDto.class);
	}
	
	public UserDto findByName(String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", name);
		Collection<UserDto> u = genericDao.findObjectsByParams(UserDto.class, map);
		if (u!=null && u.size()>0)
			return u.iterator().next();
		return null;
	}
	
}
